feat: support rebasing branches checked out in worktrees#145
Open
abersnaze wants to merge 2 commits into
Open
Conversation
When a branch is checked out in a separate git worktree, `git checkout` fails with 'already used by worktree'. Instead of checking out such branches in the main repo, detect them via `git worktree list --porcelain` and perform the rebase (or fast-forward merge) directly in the worktree directory. This handles: - Fast-forward: runs `git merge --ff-only` in the worktree - Rebase: stashes worktree changes if dirty, rebases, then unstashes Fixes the error: fatal: '<branch>' is already used by worktree at '<path>' Co-Authored-By: Oz <oz-agent@warp.dev>
When a linked worktree is in the middle of a rebase, git reports it as 'detached' in `git worktree list --porcelain` rather than pointing to the branch ref. This caused `git checkout <branch>` to fail with exit code 128 since git still considers the branch locked to that worktree. Now detached worktrees are inspected for rebase state via rebase-merge/head-name and rebase-apply/head-name, and any branch found there is reported as 'rebase in progress' and skipped. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
msiemens
reviewed
May 20, 2026
| if not os.path.isabs(meta_dir): | ||
| meta_dir = os.path.join(worktree_path, meta_dir) | ||
| meta_dir = os.path.realpath(meta_dir) | ||
| for subdir in ('rebase-merge', 'rebase-apply'): |
Owner
There was a problem hiding this comment.
Should we also handle cherry-pick/merge/bisect here? What do you think?
msiemens
reviewed
May 20, 2026
| return ref[len('refs/heads/'):] | ||
| return None | ||
|
|
||
| def _rebase_in_worktree(self, branch, target, worktree_path, |
Owner
There was a problem hiding this comment.
Could we reuse GitWrapper.rebase and GitWrapper.stasher here? It seems like they implement the same logic if I understand correctly
Owner
|
Thanks for this PR! I've left two clarifying questions as comments. Also, your commits seem to change all files.pythonhosted.org in |
Author
|
Oops sorry about that. I'll get those fixed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a branch is checked out in a separate git worktree,
git upfails with:This happens because
rebase_all_branches()tries togit checkoutthe branch, which git refuses since it's already checked out in another worktree.Solution
Before attempting checkout, build a map of branches to their worktree paths via
git worktree list --porcelain. For branches found in worktrees:git merge --ff-onlydirectly in the worktreeThis matches the existing behavior for non-worktree branches — failed rebases are left for the user to resolve.
Changes
gitup.py: Added_build_worktree_map()and_rebase_in_worktree()methods; modifiedrebase_all_branches()to dispatch worktree branches accordinglytest_worktree.py: Tests both fast-forward and rebase scenarios with a worktree branchTesting
All 37 tests pass (36 existing + 1 new).
Warp conversation
Co-Authored-By: Oz oz-agent@warp.dev